package com.java.onlineshop.mvc.handler;

import com.java.onlineshop.pojo.*;
import com.java.onlineshop.service.ICountryService;
import com.java.onlineshop.service.IProductService;
import com.java.onlineshop.service.IProvinceService;
import com.java.onlineshop.service.IUserService;
import com.java.onlineshop.service.impl.CountryServiceImpl;
import com.java.onlineshop.service.impl.ProductServiceImpl;
import com.java.onlineshop.service.impl.ProvinceServiceImpl;
import com.java.onlineshop.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.List;

/**
 * @program: onlineshop
 * @description:
 * @author: ShuangChenYue
 * @create: 2021-05-28 11:54
 **/
public class UserHandler
{
    IUserService userService = new UserServiceImpl();
//================================================进入注册页面前动态获取下拉列表中的数据====================================
    public void toRegister(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            System.out.println("通过主页点击注册来此");
            //获得国家和省份的集合
            List<Country> allCountryList = userService.findAllCountry();
            req.setAttribute("allCountryList",allCountryList);
            System.out.println(allCountryList.size());
            List<Province> allProvinceList = userService.findAllProvince();
            req.setAttribute("allProvinceList",allProvinceList);
            System.out.println(allProvinceList.size());
            //然后转发到注册页面
            req.getRequestDispatcher("/register.jsp").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//=======================================================注册方法========================================================
    public void userRegister(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException
    {
        try
        {
            req.setCharacterEncoding("utf-8");
            resp.setCharacterEncoding("utf-8");
            System.out.println("通过注册页面点击后提交数据来此方法");
            //首先先获取表单中的所有信息
            String name = req.getParameter("name");
            String password = req.getParameter("password");
            String email = req.getParameter("email");
            String phone = req.getParameter("phone");
            String country1 = req.getParameter("country");
            int countryid = Integer.parseInt(country1);
            String province1 = req.getParameter("province");
            int provinceid = Integer.parseInt(province1);
            String city = req.getParameter("city");
            String addressdetail = req.getParameter("addressdetail");

            //new一个user实例
            User user = new User();
            user.setName(name);
            user.setPassword(password);

            //new一个userinfo
            Userinfo userinfo = new Userinfo();
            user.setUserinfo(userinfo);
            userinfo.setEmail(email);
            userinfo.setPhone(phone);
            userinfo.setCity(city);
            userinfo.setAddressdetail(addressdetail);
            userinfo.setId(user.getId());
            userinfo.setUser(user);

            //new一个Country
            Country country = new Country();
            country.setId(countryid);

            //new一个province
            Province province = new Province();
            province.setId(provinceid);

            //再给info设置country和province
            userinfo.setCountry(country);
            userinfo.setProvince(province);

            //调用注册方法
            userService.userRegister(user);
            req.getRequestDispatcher("/login.jsp").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//=======================================================登录方法========================================================
    public void userLogin(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            System.out.println("点击登录来此方法");
            //获得登录表单中的所有信息
            String name = req.getParameter("name");
            String password = req.getParameter("password");
            //调用根据姓名和密码查找并返回对象
            User user = userService.userLogin(name, password);
            //若返回的对象不为空，则表示登陆成功
            if (user != null)
            {
                System.out.println("登陆成功");
                //登陆成功时，将登陆的用户存到session中
                req.getSession().setAttribute("user",user);
                IProductService productService = new ProductServiceImpl();
                List<Product> allProductList = productService.findAllProduct();
                req.setAttribute("allProductList",allProductList);
                //只要登陆成功，就创建一个购物车
                ShopCart shopCart = new ShopCart();
                //然后将购物车存进session中
                req.getSession().setAttribute("shopCart",shopCart);
                //转发到登陆成功的首页
                req.getRequestDispatcher("/loginProductList.jsp").forward(req,resp);
            }else
            {
                System.out.println("登陆失败");
            }
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//======================================================前往更新前=======================================================
    public void toUpdate(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            System.out.println("点击个人信息来此方法准备更新前的数据");
            //获得国家和省份的集合
            List<Country> allCountryList = userService.findAllCountry();
            req.setAttribute("allCountryList",allCountryList);
            System.out.println(allCountryList.size());
            List<Province> allProvinceList = userService.findAllProvince();
            req.setAttribute("allProvinceList",allProvinceList);
            req.getRequestDispatcher("/self_info.jsp").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//======================================================点击修改后=======================================================
    public void updateUser(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            ICountryService countryService = new CountryServiceImpl();
            IProvinceService provinceService = new ProvinceServiceImpl();
            req.setCharacterEncoding("utf-8");
            resp.setCharacterEncoding("utf-8");
            System.out.println("点击修改按钮后来到此方法");
            //获得表单中的所有信息
            String password = req.getParameter("password");
            String email = req.getParameter("email");
            String phone = req.getParameter("phone");
            String country = req.getParameter("country");
            int countryid = Integer.parseInt(country);
            String province = req.getParameter("province");
            int provinceid = Integer.parseInt(province);
            String city = req.getParameter("city");
            String addressdetail = req.getParameter("addressdetail");
            //将其全部存入info对象中
            User user = (User) req.getSession().getAttribute("user");
            user.setPassword(password);
            Userinfo userinfo = user.getUserinfo();
            userinfo.setEmail(email);
            userinfo.setPhone(phone);
            userinfo.setCity(city);
            //在给info设置country和province之前，先要通过id查找到后返回
            Country countryById = countryService.findCountryById(countryid);
            Province provinceById = provinceService.findProvinceById(provinceid);
            userinfo.setCountry(countryById);
            userinfo.setProvince(provinceById);
            userinfo.setAddressdetail(addressdetail);
            user.setUserinfo(userinfo);
            userinfo.setUser(user);
            //调用更新user方法
            userService.updateUser(user);
            System.out.println("调用更新方法前");
            req.getRequestDispatcher("/login.jsp").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//=======================================================退出方法========================================================
    public void logout(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            System.out.println("点击退出来此方法");
            //首先移除session中的user对象
            req.getSession().removeAttribute("user");
            //然后初始化session
            req.getSession().invalidate();
            req.getRequestDispatcher("/login.jsp").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
